package com.itjiange.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

import static com.itjiange.constant.LoggerConstant.logger;

/**
 * 放行前端资源
 *
 * @author ItJiange
 * @date 2023/10/23
 */
//@WebFilter("/*")
public class StatusFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("过滤器执行前：" + filterConfig.getFilterName());
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        List<String> urlPaths = List.of("/css/*", "/js/*", "*.html", "/user/login");
        HttpServletRequest req = (HttpServletRequest) request;
        String requestURI = req.getRequestURI();
        logger.info(requestURI);
        // 匹配到路径就放行
        if (!urlPaths.contains(requestURI)) {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
